package com.xuzimian.globaldemo.mq.rabbitmq.producer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.xuzimian.globaldemo.mq.rabbitmq.constants.Config;
import com.xuzimian.globaldemo.mq.rabbitmq.constants.ExchangeType;
import com.xuzimian.globaldemo.mq.rabbitmq.util.MqUtil;
import org.junit.jupiter.api.Test;

/***
 * @Description  rabbitmq 原生客户端，amqp协议的点对点(队列模式)的生产者
 * @author xzm
 *
 */
public class SimpleQueueProducer {

    @Test
    public  void startProducer() throws IOException, TimeoutException {
        try (Connection conn = MqUtil.createSingletonConnection()) {
            Channel channel = conn.createChannel();
            // 创建一个Direct类型，持久化，非自动删除的交换机
            channel.exchangeDeclare(Config.MQServerConect.EXCHANGE_DEMO_NAME, ExchangeType.Direct.getVal());
            // 创建一个持久化，非排他的，非自动删除的队列
            channel.queueDeclare(Config.MQServerConect.QUQUE_DEFAULT_NAME, true, false, false, null);
            // 绑定队列和交换机
            channel.queueBind(Config.MQServerConect.QUQUE_DEFAULT_NAME, Config.MQServerConect.EXCHANGE_DEMO_NAME,
                    Config.MQServerConect.ROUTING_KEY_DEFAULT_NAME);
            // 发送一条持久化消息
            channel.basicPublish(Config.MQServerConect.EXCHANGE_DEMO_NAME,
                    Config.MQServerConect.ROUTING_KEY_DEFAULT_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,
                    "hello world".getBytes());
            channel.close();
        }
    }
}